package com.pj.znt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pj.project4sp.config.SpConfigMapper;
import com.pj.utils.sg.AjaxJson;
import com.pj.xfxh.project.entity.TopicMessage;
import com.pj.xfxh.project.mapper.TopicMapper;
import com.pj.znt.domain.entity.FastGptTopic;
import com.pj.znt.mapper.FastGptTopicMapper;
import com.pj.znt.service.FastGptTopicService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户话题相关的请求。
 */
@RestController
@RequestMapping("/fastGptTopic")
public class FastGptTopicController {

    /** 底层 Mapper 对象 */
    @Autowired
    private SpConfigMapper spConfigMapper;

    /** FastGptTopic 的 Mapper 对象 */
    @Autowired
    private FastGptTopicMapper topicMapper;

    /** FastGptTopic 的服务对象 */
    @Autowired
    private FastGptTopicService topicService;

    /**
     * 根据 chatId 获取 FastGptTopic 信息。
     *
     * @param chatId 聊天 ID
     * @return 包含 FastGptTopic 信息的 ResponseEntity
     */
    @GetMapping("/{chatId}")
    public ResponseEntity<FastGptTopic> getTopicById(@PathVariable int chatId) {
        FastGptTopic topic = topicMapper.selectById(chatId);
        return ResponseEntity.ok(topic);
    }

    /**
     * 分页查询 FastGptTopic 列表。
     *
     * @param topic 查询条件
     * @param current 当前页码，默认值为 1
     * @param size 每页大小，默认值为 10
     * @return 包含分页结果的 ResponseEntity
     */
    @GetMapping("/page")
    public ResponseEntity<Page<FastGptTopic>> page(@RequestBody FastGptTopic topic,
                                                   @RequestParam(defaultValue = "1") Long current,
                                                   @RequestParam(defaultValue = "10") Long size) {
        QueryWrapper<FastGptTopic> queryWrapper = Wrappers.query();

        queryWrapper.lambda()
                .eq(topic.getUserId() != null, FastGptTopic::getUserId, topic.getUserId())
                .eq(topic.getChatId() != null, FastGptTopic::getChatId, topic.getChatId())
                .eq(topic.getPlatformId() != null, FastGptTopic::getPlatformId, topic.getPlatformId())
                .like(StringUtils.isNotEmpty(topic.getTopicName()), FastGptTopic::getTopicName, topic.getTopicName());

        Page<FastGptTopic> page = new Page<>(current, size);
        Page<FastGptTopic> topicPage = topicService.page(page, queryWrapper);

        return ResponseEntity.ok(topicPage);
    }

    /**
     * 获取所有 FastGptTopic 列表。
     *
     * @param topic 查询条件
     * @return 包含 FastGptTopic 列表的 ResponseEntity
     */
    @GetMapping("/list")
    public ResponseEntity<List<FastGptTopic>> getAllTopics(@RequestBody FastGptTopic topic) {
        QueryWrapper<FastGptTopic> queryWrapper = Wrappers.query();

        queryWrapper.lambda()
                .eq(topic.getUserId() != null, FastGptTopic::getUserId, topic.getUserId())
                .eq(topic.getChatId() != null, FastGptTopic::getChatId, topic.getChatId())
                .eq(topic.getPlatformId() != null, FastGptTopic::getPlatformId, topic.getPlatformId())
                .like(StringUtils.isNotEmpty(topic.getTopicName()), FastGptTopic::getTopicName, topic.getTopicName());

        List<FastGptTopic> topicList = topicService.list(queryWrapper);

        return ResponseEntity.ok(topicList);
    }

    /**
     * 创建新的 FastGptTopic。
     *
     * @param topic 新的 FastGptTopic 对象
     * @return 创建结果的 AjaxJson
     */
    @PostMapping("/create")
    public AjaxJson createTopic(@RequestBody FastGptTopic topic) {
        QueryWrapper<FastGptTopic> queryWrapper = Wrappers.query();
        queryWrapper.lambda()
                .eq(FastGptTopic::getUserId, topic.getUserId())
                .eq(FastGptTopic::getPlatformId, topic.getPlatformId());
        long count = topicService.count(queryWrapper);

        if (count >= 10) {
            return AjaxJson.getError("您已超过十个话题");
        }

        topicMapper.insert(topic);
        return AjaxJson.getSuccess();
    }

    /**
     * 更新 FastGptTopic 信息。
     *
     * @param topic 需要更新的 FastGptTopic 对象
     * @return 更新结果的 ResponseEntity
     */
    @PutMapping("/update")
    public ResponseEntity<String> updateTopic(@RequestBody FastGptTopic topic) {
        topicMapper.updateById(topic);
        return ResponseEntity.ok("FastGptTopic updated successfully");
    }

    /**
     * 删除 FastGptTopic。
     *
     * @param chatId 聊天 ID
     * @return 删除结果的 AjaxJson
     * @throws Exception 如果删除过程中发生异常
     */
    @DeleteMapping("/delete/{chatId}")
    public AjaxJson deleteTopic(@PathVariable int chatId) throws Exception {
        try {
            topicMapper.deleteById(chatId);
            QueryWrapper<TopicMessage> queryWrapper = Wrappers.query();
            queryWrapper.eq("topic_id", chatId);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxJson.getError("删除失败！");
        }
        return AjaxJson.getSuccess("删除成功！");
    }
}
